package com.woniu.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.ProSpence;
import com.woniu.entity.User;
import com.woniu.entity.response.ResponseEntity;
import com.woniu.entity.vo.ProSpenceExcel;
import com.woniu.entity.vo.ProSpenceVo;
import com.woniu.entity.vo.UserExcel;
import com.woniu.entity.wrapperinterface.AllProSpence;
import com.woniu.exception.controllerException.ProSpenceVoNullException;
import com.woniu.service.IProSpenceService;
import com.woniu.util.ExcelResponseHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hyl
 * @since 2022-08-28
 */
@RestController
@RequestMapping("/brain/proSpence")
@CrossOrigin(allowCredentials = "true", originPatterns = "*")
public class ProSpenceController {

    @Autowired
    private IProSpenceService pss;

    @PostMapping("/search")
    public ResponseEntity getProSpenceBySpenceByPage(@RequestBody ProSpenceVo psv){
        if (psv == null){
            throw new ProSpenceVoNullException("传入的数据为空");
        }else{
            Page<ProSpence> page = pss.getAnyProSpenceWithPage(psv.getPageNum(),psv.getPageSize(),psv);
            if (page == null){
                return ResponseEntity.fail("项目流水查询失败，系统维护中...");
            }
            List infoList = new ArrayList();
            infoList.add(page.getRecords());
            infoList.add(page.getTotal());
            infoList.add(page.getPages());
            infoList.add(page.getCurrent());
            infoList.add(page.getSize());
            return ResponseEntity.success(infoList,"查询成功");
        }
    }

    /**
     * todo 在前端使用这个端口的时候，要记住随便传些数据给psv，否则会报空
     * @param psv
     * @return
     */
    @PostMapping("/search2")
    public ResponseEntity getAllProSpenceBySpenceByPage(@RequestBody ProSpenceVo psv){
        if (psv == null){
            throw new ProSpenceVoNullException("传入的数据为空");
        }else{
            Page<ProSpence> page = pss.getAllProSpenceWithPage(psv.getPageNum(),psv.getPageSize());
            if (page == null){
                return ResponseEntity.fail("项目流水查询失败，系统维护中...");
            }
            List infoList = new ArrayList();
            infoList.add(page.getRecords());
            infoList.add(page.getTotal());
            infoList.add(page.getPages());
            infoList.add(page.getCurrent());
            infoList.add(page.getSize());
            return ResponseEntity.success(infoList,"查询成功");
        }
    }

    @PostMapping("/download")
    public void downloadExcel(@RequestBody(required = false) ProSpenceVo psv, HttpServletResponse response) throws IOException {
        List<ProSpence> psList = pss.getAnyProSpences(psv);
        List<ProSpenceExcel> proSpenceExcels = new CopyOnWriteArrayList<>();
        for(ProSpence ps : psList){
            ProSpenceExcel excel = new ProSpenceExcel();
            excel.setSpenId(ps.getSpenId());
            excel.setProId(ps.getProId());
            excel.setCateId(ps.getCateId());
            excel.setSpenDate(ps.getSpenDate());
            excel.setSpenPrice(ps.getSpenPrice());
            proSpenceExcels.add(excel);
        }
        // todo 细化ProSpenceExcel的属性，使得导出的时候能够看到与项目流水相关的所有信息
        ExcelResponseHeaderUtil.setExcelRespProp(response, "项目流水表");
        EasyExcel.write(response.getOutputStream())
                .head(UserExcel.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("项目流水表")
                .doWrite(proSpenceExcels);
    }

    // todo 2022-09-01 23:54 明天任务，通过传入的文件导入到数据库
}
